<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Pro;
use App\Att;
use App\User;
use DB;
use App\Bid;
use Auth;
use Validator;

class ProController extends Controller
{
    //echo 123;exit;
    protected $middleware = ['App\Http\Middleware\Authenticate' => []];
    public function jie(){
        return view('woyaojiekuan');
    }
    public function jiePost(Request $req){
        //自动验证
        /*$this->validate($req , [
                'age'=>'required|in:15,40,80',
                'money'=>'required|digits_between:2,7',
                'mobile'=>'required|regex:/^1[3458]/',
            ],

            [
                'money.required'=>'借款金额必须填写',
                'money.digits_between'=>'借款金额填的不对啊',
                'in'=>':attribute 必须是 :values 之一',
                'mobile.required'=>'手机号码必须填写',
                'mobile.regex'=>':attribute 不合要求'
            ]

            );*/
            //手动验证
            $validate = validator::make($req->all() , 

            [
                'age'=>'required|in:15,40,80',
                'money'=>'required|digits_between:2,7',
                'mobile'=>'required|regex:/^1[3458]/',
            ],

            [
                'money.required'=>'借款金额必须填写',
                'money.digits_between'=>'借款金额填的不对啊',
                'in'=>':attribute 必须是 :values 之一',
                'mobile.required'=>'手机号码必须填写',
                'mobile.regex'=>':attribute 不合要求'
            ]
            );
                if( $validate->fails() ){
                    return back()->withErrors($validate)->withInput();
                }
            
        //$user = Auth::user();//获取当前用户的信息,跟下面的直接使用没什么区别
        $user = $req->user();
        //return 'insert into DB';
        $pro = new Pro();
        $att = new Att();
        //主表
        $pro->money = $req->money * 100;
        $pro->mobile = $req->mobile;
        $pro->pubtime = time();
        $pro->uid = $user->uid;
        $pro->name = $user->name;
        //print_r($pro);exit;
        $pro->save();
        //附表
        $att->age = $req->age;
        $att->pid = $pro->pid;
        $att->uid = $pro->uid;
        
        $att->save();
        //print_r($att);exit;
    }

   /*我的账单*/
    public function myzd(){
        $user = Auth::user();
        $uid = $user->uid;
        $hks = DB::table('hks')->where('uid',$uid)->get();
        //dd($hks);exit;
        return view('myzd' , ['hks' => $hks]);
    }

    /*我的收益*/
    public function mysy(){
        $user = Auth::user();
        $uid = $user->uid;
        $grows =DB::table('grows')->where('uid',$uid)->orderBy('gid' , 'desc')->paginate(2);
        $grows =DB::table('grows')->where('uid',$uid)->orderBy('gid' , 'desc')->get();
        //dd($uid);exit;
        return view('mysy' , ['grows'=>$grows]);
    }

    /*我的投资*/
    public function mytz(){
        $user = Auth::user();
       
        $bids = DB::table('bids')
                ->join('projects', 'projects.pid', '=', 'bids.pid')
                ->select('bids.*','projects.status')
                ->where('bids.uid',$user->uid)
                ->get();
 //dd($bids);exit;
        return view('mytz' , ['mytz'=>$bids]);
    }

}